package com.hlf.modules.api.service.impl;


import com.hlf.common.exception.BusinessException;
import com.hlf.common.utils.IdWorkerFactory;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.mapper.UserMapper;
import com.hlf.modules.api.entity.UserEntity;
import com.hlf.modules.api.service.UserService;
import com.hlf.modules.merchant.merchant.entity.Merchant;
import com.hlf.modules.merchant.merchant.service.MerchantService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;



@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MerchantService merchantService;

    @Override
    public UserEntity queryObject(Long userId){
        return userMapper.queryObject(userId);
    }

    @Override
    public List<UserEntity> queryList(Map<String, Object> map){
        return userMapper.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map){
        return userMapper.queryTotal(map);
    }

    @Override
    public void save(String mobile, String password){
        UserEntity user = new UserEntity();
        user.setMobile(mobile);
        user.setUsername(mobile);
        user.setPassword(DigestUtils.sha256Hex(password));
        user.setCreateTime(new Date());
        userMapper.save(user);
    }

    @Override
    public void update(UserEntity user){
        userMapper.update(user);
    }

    @Override
    public void delete(Long userId){
        userMapper.delete(userId);
    }

    @Override
    public void deleteBatch(Long[] userIds){
        userMapper.deleteBatch(userIds);
    }

    @Override
    public UserEntity queryByMobile(String mobile) {
        return userMapper.queryByMobile(mobile);
    }

    @Override
    public long login(String mobile, String password) {
        UserEntity user = queryByMobile(mobile);
        Assert.isNull(user, "手机号或密码错误");

        //密码错误
        if(!user.getPassword().equals(DigestUtils.sha256Hex(password))){
            throw new BusinessException("手机号或密码错误");
        }

        return user.getUserId();
    }


    /***
     *检查手机号是否存在
     * @param l
     * @param mobile
     * @param userType
     * @return
     */
    @Override
    public boolean checkMobileExist(long l, String mobile, int userType) {
        return false;
    }

    @Override
    public void saveUserAndMerchant(String mobile, String password) {
        UserEntity user = new UserEntity();
        user.setMobile(mobile);
        user.setUsername(mobile);
        user.setPassword(DigestUtils.sha256Hex(password));
        user.setCreateTime(new Date());
        userMapper.save(user);

        Merchant merchant=new Merchant();
        merchant.setId(IdWorkerFactory.nextId());
        merchant.setUserId(user.getUserId());
        merchant.setContactPhone(mobile);
        merchantService.save(merchant);
    }

    /**
     * 忘记密码
     * @param mobile
     * @param password
     * @return
     */
    @Override
    public Integer forgetPassword(String mobile, String password) {

        UserEntity userEntity = userMapper.selectByMobile(mobile);
        userEntity.setPassword(DigestUtils.sha256Hex(password));
        Integer i = userMapper.updateByPrimaryKeySelective(userEntity);

        return i;
    }

    /**
     * 查询手机号存在个数
     * @param mobile
     * @return
     */
    @Override
    public Integer mobileCount(String mobile) {
        return userMapper.selectCountByMobile(mobile);
    }
}
